library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(stargazer)


rm(list=ls())
home = 'C:/Users/Jason/Dropbox/VNA_Responsiveness/Analysis/JOP-dataverse/'


permutation.results = paste0(home, 'RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'experimental-analyses.Rds') %>%
  readRDS


columns = c('tab3col1','tab3col2','tab3col3','tab3col7')
significance = subset(permutation.results, 
                      Result %in% columns) %>%
  dlply(.variables='Result', 
        .fun=function(column) {
          ddply(.data=column,
                .variables='term',
                .fun=function(x) {
                  est = subset(experimental.results, 
                               Result==x$Result[1] & term==x$term[1])$estimate
                  ris = x$estimate
                  qest = ecdf(ris)
                  Q = qest(est) %>%
                    round(digits=3) %>%
                    str_pad(width=5, side='right', pad=0) %>%
                    paste0('(', ., ')')
                  est = est %>%
                    round(digits=3) %>%
                    str_pad(width=5, side='right', pad=0)
                  data.frame(output=paste0(est, '\n', Q),
                             stringsAsFactors=F) %>%
                    set_colnames(x$Result[1])
                }, .inform=T)
        }, .progress='text', .inform=T)


output = merge(significance[['tab3col1']], significance[['tab3col2']], by='term', all=T) %>%
  merge(significance[['tab3col3']], by='term', all=T) %>%
  merge(significance[['tab3col7']], by='term', all=T) %>%
  set_rownames(.[,'term']) %>%
  subset(select=-term)
output = output[c('Citizen',
         'Prop.Citizen',
         'Citizen:Prop.Citizen',
         'Firm',
         'Prop.Firm',
         'Firm:Prop.Firm',
         'FullTime',
         'CentNom',
         'Competitive',
         '(Intercept)'),] %>%
  adply(.margins=1, .fun=function(x) {
    x1 = str_extract(string=unlist(x), pattern='.*(?=\\n)')
    x2 = str_extract(string=unlist(x), pattern='(?<=\\n).*')
    rbind(x1, x2) %>%
      set_colnames(colnames(x))
  }) %>%
  sapply(FUN=as.character) %>%
  as.data.frame(stringsAsFactors=F)


c(subset(experimental.results, Result=='tab3col1')$N[1],
  subset(experimental.results, Result=='tab3col2')$N[1],
  subset(experimental.results, Result=='tab3col3')$N[1],
  subset(experimental.results, Result=='tab3col7')$N[1]) %>%
  matrix(nrow=1) %>%
  as.data.frame %>%
  set_colnames(colnames(output)) %>%
  rbind(output, .) %>%
  set_rownames(c('Citizen','1',
                 '% Citizen','2',
                 'Citizen times % Citizen','3',
                 'Firm','4',
                 '% Firm','5',
                 'Firm times % Firm','6',
                 'Full-time','7',
                 'Central nominee','8',
                 'Competitive','9',
                 'Constant','10',
                 'Observations')) %>%
  stargazer(summary=F, 
            header=F,
            align=T,
            notes.append=F,
            notes='Randomization inference based on 10,000 randomizations. Quantile of experimental estimate in parentheses.',
            title='Citizen-treated delegates more likely to speak (Paper Figure 6; Figure 8.3).')

